/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-20  이윤노    Created.  
*  2012-06-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){

	//라운드목록
	function clsClubRoundList(){
		this._HTMLDom = null;
		this.roundList = null;
		
		this._init();	
	}
	
	clsClubRoundList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.self_home_joinroom");
			this._HTMLDom = {
				"$layer": $layer	
				, "$element": $layer.find("ul li:first")
			}
			
			$layer.find("ul").empty();
			$("div#container section#main_content article.board_list_none").hide();
		}
		, setList: function(thisData){
			this.roundList = thisData;
			this.display();
		}
		, addProfile: function(n, thisData){
			this.roundList[n]["프로필"] = thisData;
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"].find("ul");
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			
			this.isNoData();
			
			for(var i=0;i<this.roundList.length;++i){
				$layer.append($element.clone(true));	
			}
			
			$layer.find("li").each(function(n){
				$(this).find("td.nickname").html(thisClass.roundList[n]["닉네임"]).hover(function(event){
					_gf.showProfileLayer($(this), thisClass.roundList[n]["프로필"], "left", 0, 33);
				}, function(event){
					_gf.closeProfileLayer();
				});
				$(this).find("td.time").contents().filter(function(){
					return (this.nodeType==3&&this.data.trim());
				}).after(getFormatDateYmd(thisClass.roundList[n]["라운드일자"],".") + "(" + thisClass.roundList[n]["라운드요일"] + ")").remove();
				$(this).find("td.time span").html(thisClass.roundList[n]["라운드시간"]);
				
				$(this).find("td:eq(2)").html(thisClass.roundList[n]["코스명"]);
				$(this).find("td:eq(3)").html(thisClass.roundList[n]["홀수"] + "H");
				$(this).find("td.align_l:eq(0)").css("cursor", "pointer").html(thisClass.roundList[n]["라운드명"]).click(function(event){
					event.preventDefault();			
					document.objControl.goView(n);
				});
				$(this).find("td:eq(5) strong").html(thisClass.roundList[n]["모집팀수"] + "팀");
				$(this).find("td:eq(5)").contents().filter(function(){
					return (this.nodeType==3&&this.data.trim());
				}).after("("+thisClass.roundList[n]["모집인원수"]+"명)").remove();
				$(this).find("td.align_l span.offer:eq(0)").html(thisClass.roundList[n]["신청인원수"]);
				$(this).find("td.align_l span.offer:eq(1)").html(thisClass.roundList[n]["배정완료수"]);
			
				if(thisClass.roundList[n]["라운딩진행상태"]=="P"){
					$(this).find("td.ing img").attr("src", "../images/btn/btn_ing.png");
				}else if(thisClass.roundList[n]["라운딩진행상태"]=="C"){
					$(this).find("td.ing img").attr("src", "../images/btn/btn_ok.png");
				}else if(thisClass.roundList[n]["라운딩진행상태"]=="D"){
					$(this).find("td.ing img").attr("src", "../images/btn/btn_end.png");
				}
				
			});
		}
		, spinStart: function(){
			
		}
		, spinEnd: function(){
			
		}
		, isNoData: function(){
			var $layer = $("div#container section#main_content article.board_list_none").html("++ 등록된 동호회 정기라운드가 없습니다. ++");
			if(this.roundList.length<=0) {
				$layer.show();
				$("#idClubRoundListPage").hide();
			}else {
				$layer.hide();
				$("#idClubRoundListPage").show();
			}
		}
	}
	
	
	function clsComm()
	{
		this.clubRoundList = null;
		this.clubRoundListPageData = null;
		this.clubRoundListRequest = null;		
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.clubRoundListRequest = {
				 "동호회키":null
				, "라운드키": null
				, "페이지": null				
			}
			
			this.clubRoundListPageData = {
				"전체수": null	
			}
		}
//------------------------ 라운드목록------------------------//		
		, getClubRoundList: function(){
			var thisClass = this;
			var szClubSeq = this.clubRoundListRequest["동호회키"];
			var szPage = this.clubRoundListRequest["페이지"];
			
			// ajax 시작			
			//parameter setting and run
			_gf.ajax({
				"sid":"CL001",
				"servicename":"getClubRoundList",
				"listcount":5,
				"currpageno":szPage
			}, {
				  "clubSeq": szClubSeq
			}, function(ret){
				
				// callback and mapping
				thisClass.clubRoundList = [];
			
				if(ret["msp"]["contents"]["items"]){					
					thisClass.clubRoundListPageData = {
						"전체수": ret["msp"]["contents"]["@totalcount"]
					}					
					
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"] = new Array(ret["msp"]["contents"]["items"]);
					}
					
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){
						var obj = ret["msp"]["contents"]["items"][i];
						
						//동호회목록
						thisClass.clubRoundList.push({
							"동호회키": _util.nvl(obj["roundInfo"]["clubSeq"])
							, "아이디": _util.nvl(obj["userMaster"]["userId"])
							, "닉네임": _util.nvl(obj["userMaster"]["nickname"])
							, "라운드키": _util.nvl(obj["roundInfo"]["roundSeq"])
							, "라운드일자": _util.nvl(obj["roundInfo"]["reservDate"])
							, "라운드요일": _util.nvl(obj["roundInfo"]["reservDay"])
							, "라운드시간": _util.nvl(obj["roundInfo"]["firstTime"])
							, "코스명": _util.nvl(obj["roundInfo"]["golfclubCourseInfo"])
							, "홀수": _util.nvl(obj["roundInfo"]["holeInfo"])
							, "라운드명": _util.nvl(obj["roundInfo"]["title"])
							, "모집팀수": _util.nvl(obj["roundInfo"]["totalTeamCnt"])
							, "모집인원수": _util.nvl(obj["roundInfo"]["totalMember"])
							, "신청인원수": _util.nvl(obj["roundInfo"]["requestedMemberCnt"])
							, "배정완료수": _util.nvl(obj["roundInfo"]["assignedMemberCnt"])
							, "라운딩진행상태": _util.nvl(obj["roundInfo"]["procStatus"]) //진행중(0),확정(1),마감(2)					
							, "프로필": []								
						});
					}         
				}
				
				$(thisClass).trigger("evtFinishClubRoundList");
			});
		}
		, getClubRoundListData: function(){
			return this.clubRoundList;
		}
		, getClubRoundListPageData: function(){
			return this.clubRoundListPageData;
		}
		, setClubRoundListRequest: function(key, val){
			this.clubRoundListRequest[key] = val;
		}
		, getClubRoundListRequest: function(key){
			return this.clubRoundListRequest[key];
		}	
//------------------------ 라운드목록------------------------//	
	}
	
	function clsControl()
	{
		this.objComm = null;
		
		this.objClubInfo = null
		this.objRole = null;
		
		this.objClubRoundList = null;
		this.objClubRoundListPage = null;
		
		this.club_seq = null;
		this.club_my = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			var member_id = document.objUtil.getCookie("member_id");
			this.club_seq = document.objUtil.getParam("club_seq");
			this.club_my = (document.objUtil.getParam("club_my") == "Y") ? 1:0;
			
			if(!this.club_seq) document.location.href = "../club/club.html";
			
			if(!this.club_seq) {
				document.location.href = "../club/club.html";
			}
			
			this._preload();
			
			this.objComm = new clsComm();
			this.objClubRoundList = new clsClubRoundList();
			this.objClubRoundListPage = new _gf.makePaging($("#idClubRoundListPage"), 5, 10);
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			$.ajax({
				url: "js/self_club_top.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return (_clubControl.getClubInfo().infoData);
			}, function(){				
				_gf.spinEnd();;
				thisClass._start();
			});	
		}
		, _role: function(){			
			if(!this.objRole.isAdmin()){
				$("section#main_content h5 span").hide();
			}
		}
		, _start: function(){		
			this.objClubInfo = _clubControl.getClubInfo();
			this.objRole = new clsRole(this.objClubInfo.infoData);
			this._role();
			
			//게시판글목록 조회
			this.objClubRoundList.spinStart();
			this.objComm.setClubRoundListRequest("동호회키",this.club_seq);
			this.objComm.setClubRoundListRequest("페이지",1);
			this.objComm.getClubRoundList();
			this.objClubRoundListPage.setPage(1);
		}
		, _bind: function(){
			var thisClass = this;
		
			//동호회 글목록조회  완료 이벤트 
			$(this.objComm).bind("evtFinishClubRoundList", function(){
				var thisData = thisClass.objComm.getClubRoundListData()
				var pageData = thisClass.objComm.getClubRoundListPageData();
				
				thisClass.objClubRoundList.spinEnd();
				thisClass.objClubRoundList.setList(thisData);
				
				// 페이지 부에 전체 수설정 				
				thisClass.objClubRoundListPage.setTotalCount(pageData["전체수"]);
				thisClass.objClubRoundListPage.display();
				
				//프로필
				for(var i=0;i<thisData.length;++i){									
					_gf.getMemberInfo({
						"아이디": thisData[i]["아이디"]
						, "인덱스": i
						, "콜백": function(res, info, arg){
							thisClass.objClubRoundList.addProfile(arg["인덱스"], info);
						}
					});
				}
			});
			
			// 페이지이동 이벤트
			$(this.objClubRoundListPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setClubRoundListRequest("페이지", thisPage);
				thisClass.objClubRoundList.spinStart();
				thisClass.objComm.getClubRoundList();
			});
						
			//라운드생성
			$("div#container section#main_content span.fr a").click(function(event){
				event.preventDefault();						
				if(!thisClass.objRole.isAdmin()){
					alert(thisClass.objRole.getError());	
					return;
				}
				document.location.href = "club_admin_rd_creat.html?club_seq=" + thisClass.club_seq;
			});
		}
		, goView: function(n){
			var member_id = _util.getCookie("member_id");
			
			if(!member_id) {				
				if(confirm("로그인 후 이용하여 주세요.")){
					_gf.goLogin();	
				}else {
					return;	
				}
			}else if(!this.objRole.isMember()){
				alert(this.objRole.getError());
				return;	
			}else {
				document.location.href = "club_admin_rd_view.html?club_seq=" + this.objClubRoundList.roundList[n]["동호회키"] + "&round_seq="+ this.objClubRoundList.roundList[n]["라운드키"];
			}
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});